package ind.nuts.lang.entity;

import cn.hutool.core.bean.BeanUtil;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

import static java.util.stream.Collectors.toList;

/**
 * @author haoyun.zheng
 */
@Data
@AllArgsConstructor
public class PageVO<T> implements Serializable {
    private static final long serialVersionUID = 207599187753509078L;

    private Integer total;
    private List<T> items;

    public static <T, E> PageVO<T> of(Long total, List<E> items, Class<T> clazz) {
        // 翻页查询时, 一般来说Integer长度就够用, 这里对Long进行转换。
        if (Integer.MAX_VALUE >= total) {
            List<T> convertItems = items.stream().map(e -> BeanUtil.toBean(e, clazz)).collect(toList());
            return new PageVO<>(total.intValue(), convertItems);
        } else {
            throw new IllegalArgumentException("翻页查询时, 总页数长度超过有效值。");
        }
    }

}
